package com.dx.weichat.weichat.service;

import com.dx.weichat.weichat.entity.CustomEventMsg;
import com.dx.weichat.weichat.entity.LocationEventMsg;
import com.dx.weichat.weichat.entity.TextMsg;
import com.dx.weichat.weichat.redisUtils.RedisUtil;
import com.dx.weichat.weichat.wechatUtils.JsonUtil;
import com.dx.weichat.weichat.wechatUtils.WSUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: dx
 * @date: 2018/10/8/0008
 * @description: 事件类型消息处理Serveice
 */
@Service
public class EventMsgService {
	private static Logger logger = LoggerFactory.getLogger(EventMsgService.class);
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private CommonMsgService commonMsgService;
	/**
	 *@author:dx
	 * @date:2018/10/8/0008
	 * @time:12:11
	 * @description:自定义消息处理方法
	 * @param customEventMsg
	 * @return
	 */
	public String processCustomMsg(CustomEventMsg customEventMsg){
		String responseTextMsg = "";
		//从redis中取出位置信息
		LocationEventMsg currentEventMsg = (LocationEventMsg)redisUtil.get(customEventMsg.getFromUserName());
		logger.info("当前用户ID:{} 位置:经度={},纬度={}",currentEventMsg.getFromUserName(),currentEventMsg.getLongitude(),currentEventMsg.getLatitude());
		//responseTextMsg = JsonUtil.Obj2xmlByGson(new TextMsg(MessageFormat.format("你好啊,朋友 你的位置 经度:{0},纬度:{1}",currentEventMsg.getLongitude(),currentEventMsg.getLatitude()), currentEventMsg));
		logger.info("当前用户所在城市:{}",WSUtil.getCityByll(currentEventMsg.getLongitude(),currentEventMsg.getLatitude()));
		String currentUserCityCode = WSUtil.getCityByll(currentEventMsg.getLongitude(),currentEventMsg.getLatitude());
		responseTextMsg = JsonUtil.Obj2xmlByGson(new TextMsg(commonMsgService.handleWeather(currentUserCityCode),currentEventMsg));
		return responseTextMsg;

	}
}
